package com.study.jyg.rxjava2study.activity;

import android.os.CountDownTimer;
import android.util.Log;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.adapter.MouseAdapter;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.utils.DensityUtil;
import com.study.jyg.rxjava2study.utils.MyMath;
import com.study.jyg.rxjava2study.utils.TimerUtils;
import com.study.jyg.rxjava2study.widget.UniversalItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 游戏 打地鼠
 */
public class GameActivity extends BasicActivity {

    private final long GAME_TIME = 1000 * 120;
    private final long GAME_TIMER = 3;

    private TextView tvTime;
    private TextView tvStart;
    private TextView tvStop;
    private RecyclerView recyclerView;
    private MouseAdapter adapter;


    private long gameTime;
    private long gameStartTime;
    private boolean isStart;
    private int randomNumb = -1;

    private CountDownTimer timer;


    @Override
    protected int getLayout() {
        return R.layout.activity_game;
    }

    @Override
    protected void initView() {
        tvTime = findViewById(R.id.tvTime);
        tvTime.setText(String.format("TIME：%s", GAME_TIME/1000));
        tvStart = findViewById(R.id.tvStart);
        tvStop = findViewById(R.id.tvStop);
        recyclerView = findViewById(R.id.recyclerView);
        initAdapter();
        gameStartTime = GAME_TIME;
        tvStart.setOnClickListener(v -> {
            if (!isStart)
                startGame();
        });
        tvStop.setOnClickListener(v -> {
            if (isStart)
                pauseGame();
        });
    }

    private void initAdapter() {
        ((DefaultItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        List<MouseBean> list = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            list.add(new MouseBean(0));
        }
        adapter = new MouseAdapter();
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(adapter);
        adapter.setNewData(list);
        recyclerView.addItemDecoration(new UniversalItemDecoration() {
            @Override
            public Decoration getItemOffsets(int position) {
                ColorDecoration decoration = new ColorDecoration(R.color.colorLucency);
                decoration.bottom = DensityUtil.dip2px(20);
                decoration.left = DensityUtil.dip2px(5);
                decoration.right = DensityUtil.dip2px(5);
                return decoration;
            }
        });
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            if (adapter.getData().get(position).getType() == 1){
                Log.e("case", "case=1 positon="+position+ " itemClick");
                adapter.setData(position, new MouseBean(2));
            }else {

            }
        });
    }

    /**
     * 开始游戏
     */
    private void startGame() {
        isStart = true;
        if (gameStartTime == 0) gameTime = GAME_TIME;
        else gameTime = gameStartTime;
        timer = TimerUtils.countDown(gameTime, TimerUtils.SECOND, new TimerUtils.CountDownTimerListener() {
            @Override
            public void onTick(long l) {
                tvTime.setText(String.format("TIME：%s", l>10?l:"0"+l));
                gameStartTime = l;
                if (l % GAME_TIMER == 0) {
                    //出老鼠
                    getRandomNumb();
                    adapter.setData(randomNumb-1, new MouseBean(1));
                }
            }

            @Override
            public void onFinish() {
                isStart = false;
            }
        });
    }

    /**
     * 获取随机数
     */
    private void getRandomNumb(){
        int numb = MyMath.random(adapter.getItemCount());
        if (numb == randomNumb)
            getRandomNumb();
        else
            randomNumb = numb;
    }

    /**
     * 暂停游戏
     */
    private void pauseGame() {
        isStart = false;
        if (timer != null) timer.cancel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) timer.cancel();
        timer = null;
    }

    public static class MouseBean {
        private int type;

        public MouseBean(int type) {
            this.type = type;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }
    }
}